package com.xuetang9.t9.xinhuaxia.alipay;

import com.xuetang9.t9.xinhuaxia.beans.AliReturnPay;
import com.xuetang9.t9.xinhuaxia.service.IUserOrderListService;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author 李亚奇
 * @version 1.0
 * @desc TODO
 * @date 2020/3/10 11:16
 * @copyright liyq
 * @address 成都西部国际金融中心2栋2201
 **/
@Controller
@RequestMapping(value = "returnPay")
public class ReturnController {
    @Autowired
    private IUserOrderListService userOrderListService;
    /**
     * 支付宝回调的接口
     * @param
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/aliReturnPay", method = RequestMethod.POST)
    public void returnPay(HttpServletResponse response, AliReturnPay returnPay, HttpServletRequest req)
            throws IOException {
        response.setContentType("type=text/html;charset=UTF-8");
        System.out.println("**支付宝的的回调函数被调用***");
        if (!PayUtil.checkSign(req)) {
            System.out.println("****************************************验签失败*******************************************");
            response.getWriter().write("failture");
            return;
        }
        if (returnPay == null) {
            System.out.println("支付宝的returnPay返回为空");
            response.getWriter().write("success");
            return;
        }
        System.out.println("支付宝的returnPay" + returnPay.toString());
        if (returnPay.getTrade_status().equals("TRADE_SUCCESS")) {
            System.out.println("支付宝的支付状态为TRADE_SUCCESS");
            System.out.println(returnPay);
//            tbPaymentRecordsService.aliPaySuccess(returnPay);
        }
        response.getWriter().write("success");
    }
}
